package com.tusuo.stds.ui.linkaccount;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;

import com.andreabaccega.widget.FormEditText;
import com.tusuo.stds.R;
import com.tusuo.stds.rest.ModuleList;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.ui.login.ThirdLoginUserInfo;
import com.tusuo.stds.util.Utils;
import com.tusuo.stds.util.validator.MobilePhoneNumberValidator;
import com.tusuo.stds.util.validator.PasswordValidator;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.ColorRes;
import org.androidannotations.annotations.res.StringRes;
import org.parceler.Parcels;

import java.util.ArrayList;
import java.util.List;

import myandroid.library.util.ApplicationHelper;

@EActivity(R.layout.activity_link_account)
public class LinkAccountActivity extends BaseToolbarActivity<LinkAccountView, LinkAccountPresenter>
                                implements LinkAccountView {

    @ViewById(R.id.tv_msg)
    TextView tvMsg;
    @ViewById(R.id.et_mobile_num)
    FormEditText etMobileNum;
    @ViewById(R.id.et_password)
    FormEditText etPassword;
    @ViewById(R.id.link_account_btn_link)
    Button btnLink;

    @StringRes(R.string.toolbar_title_link_account)
    String toolbarTitle;
    @StringRes(R.string.err_msg_mobile_number)
    String strErrMsgMobileNumber;
    @StringRes(R.string.err_msg_password)
    String strErrMsgPassword;

    @ColorRes(R.color.colorPrimary)
    int toolbarBgColor;
    @ColorRes(R.color.textColorPrimary)
    int toolbarTextColor;

    private ThirdLoginUserInfo mThirdLoginUserInfo;

    @AfterInject
    void init() {
        mThirdLoginUserInfo = Parcels.unwrap(getIntent().getParcelableExtra(ThirdLoginUserInfo.EXTRA_NAME));
    }

    @AfterViews
    void afterViews() {
        setupUI(this, toolbarBgColor, toolbarTextColor);

        List<ToolbarOwner.MenuAction> menuActionList = new ArrayList<>();

        ToolbarOwner.Config config = new ToolbarOwner.Config(toolbarTitle, true, R.drawable.ic_back_dark, menuActionList);
        mToolbarOwner.setConfig(config);

        btnLink.setEnabled(false);

        etMobileNum.addTextChangedListener(new EditTextChangedListener());
        etPassword.addTextChangedListener(new EditTextChangedListener());

        etMobileNum.addValidator(new MobilePhoneNumberValidator(strErrMsgMobileNumber));
        etPassword.addValidator(new PasswordValidator(strErrMsgPassword));

        ApplicationHelper.getInstance().addActivity(this);
    }


    @NonNull
    @Override
    public LinkAccountPresenter createPresenter() {
        return new LinkAccountPresenter(this);
    }

    @Override
    public void showErrMsg(String text) {
        tvMsg.setText(text);
        tvMsg.setTextColor(Color.RED);
    }

    @Override
    public void showSlogan() {
        tvMsg.setText(getString(R.string.slogan));
        tvMsg.setTextColor(Color.WHITE);
    }

    @Click(R.id.link_account_btn_reset_password)
    void clickResettingPasswordBtn() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        presenter.resetPassword();
    }

    @Click(R.id.link_account_btn_link)
    void clickLinkBtn() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        // 强制隐藏软键盘
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

        if (!etMobileNum.testValidity()) {
            presenter.showErrMsg(strErrMsgMobileNumber);
            return;
        }

        if (!etPassword.testValidity()) {
            presenter.showErrMsg(strErrMsgPassword);
            return;
        }

        presenter.linkAccount(etMobileNum.getText().toString(),
                etPassword.getText().toString(),mThirdLoginUserInfo);

      /*  presenter.linkAccount(etMobileNum.getText().toString(),
                etPassword.getText().toString(),
                mThirdLoginUserInfo.openId,
                mThirdLoginUserInfo.platForm,
                mThirdLoginUserInfo.accessToken,
                mThirdLoginUserInfo.nickName);*/
    }


    private class EditTextChangedListener implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (etMobileNum.getText().length() > 0 && etPassword.getText().length() > 0) {
                btnLink.setBackgroundResource(R.drawable.selector_theme_button);
                btnLink.setEnabled(true);
            } else {
                btnLink.setBackgroundResource(R.drawable.shape_theme_button_disabled);
                btnLink.setEnabled(false);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    }
}
